package fw_ws_client

import (
	"context"
	"github.com/duke-git/lancet/v2/strutil"
	"github.com/togettoyou/wsc"
	"log"
	"time"
	"transport/framework/fw_db/fw_db_thms"
	"transport/framework/fw_log"
	"transport/web/web_thms_mapper"
	"transport/web/web_thms_model"
)

func StartWsClient() {
	ws := wsc.New("wss://www.coldcloud.com:30000")
	// 可自定义配置，不使用默认配置
	//ws.SetConfig(&wsc.Config{
	//	// 写超时
	//	WriteWait: 10 * time.Second,
	//	// 支持接受的消息最大长度，默认512字节
	//	MaxMessageSize: 2048,
	//	// 最小重连时间间隔
	//	MinRecTime: 2 * time.Second,
	//	// 最大重连时间间隔
	//	MaxRecTime: 60 * time.Second,
	//	// 每次重连失败继续重连的时间间隔递增的乘数因子，递增到最大重连时间间隔为止
	//	RecFactor: 1.5,
	//	// 消息发送缓冲池大小，默认256
	//	MessageBufferSize: 1024,
	//})
	// 设置回调处理
	ws.OnConnected(func() {
		log.Println("OnConnected: ", ws.WebSocket.Url)
		err := ws.SendTextMessage("483")
		log.Println("OnConnected: ", err)
		// 连接成功后，测试每50秒发送消息
		go func() {
			t := time.NewTicker(50 * time.Second)
			for {
				select {
				case <-t.C:
					err := ws.SendTextMessage("483")
					if err == wsc.CloseErr {
						return
					}
				}
			}
		}()
	})
	ws.OnConnectError(func(err error) {
		log.Println("OnConnectError: ", err.Error())
	})
	ws.OnDisconnected(func(err error) {
		log.Println("OnDisconnected: ", err.Error())
	})
	ws.OnClose(func(code int, text string) {
		log.Println("OnClose: ", code, text)
	})
	ws.OnTextMessageSent(func(message string) {
		//log.Println("OnTextMessageSent: ", message)
	})
	ws.OnBinaryMessageSent(func(data []byte) {
		log.Println("OnBinaryMessageSent: ", string(data))
	})
	ws.OnSentError(func(err error) {
		log.Println("OnSentError: ", err.Error())
	})
	ws.OnPingReceived(func(appData string) {
		log.Println("OnPingReceived: ", appData)
	})
	ws.OnPongReceived(func(appData string) {
		log.Println("OnPongReceived: ", appData)
	})
	ws.OnTextMessageReceived(func(message string) {
		// 解析温湿度数据
		thmsData := strutil.SplitEx(message, ",", true)
		gpsData := strutil.SplitEx(message, "|", true)
		if len(thmsData) > 1 {
			thmsMapper := web_thms_mapper.ThmsMapper{
				DB:   fw_db_thms.GetSqliteDB(),
				Lock: fw_db_thms.GetSqliteLock(),
				Ctx:  context.Background(),
			}
			thmsModel := new(web_thms_model.YtlThms)
			if len(thmsData) > 5 {
				thmsModel.CustomerID = thmsData[0]
				thmsModel.DeviceID = thmsData[1]
				thmsModel.TtIndex = thmsData[2]
				thmsModel.Wd = thmsData[3]
				thmsModel.Sd = thmsData[4]
				thmsModel.Flag = thmsData[5]
			}
			if len(thmsData) > 7 {
				thmsModel.Volt = thmsData[6]
				thmsModel.Dbm = thmsData[7]
			}
			_, err := thmsMapper.Create(thmsModel)
			if err != nil {
				fw_log.Slogger.Info("----温湿度数据存储错误---", err.Error())
			}
		}
		if len(gpsData) > 1 {
			gpsMapper := web_thms_mapper.GpsMapper{
				DB:   fw_db_thms.GetSqliteDB(),
				Lock: fw_db_thms.GetSqliteLock(),
				Ctx:  context.Background(),
			}
			gpsModel := new(web_thms_model.YtlGps)
			if len(gpsData) > 3 {
				gpsModel.CustomerID = gpsData[0]
				gpsModel.DeviceID = gpsData[1]
				gpsModel.Lat = gpsData[2]
				gpsModel.Lng = gpsData[3]
			}
			_, err := gpsMapper.Create(gpsModel)
			if err != nil {
				fw_log.Slogger.Info("----GPS数据存储错误---", err.Error())
			}
		}
	})
	ws.OnBinaryMessageReceived(func(data []byte) {
		log.Println("OnBinaryMessageReceived: ", string(data))
	})
	// 开始连接
	go ws.Connect()
}
